﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;

namespace MDCAdminTool.Controls.Data_Cache.ViewModels
{
    public class AddDataCacheItemViewModel : INotifyPropertyChanged
    {
        #region Key
        private string _key;
        public string Key
        {
            get { return _key; }
            set { _key = value; OnPropertyChanged("Key"); }
        } 
        #endregion

        #region Value
        private string _value;
        public string Value
        {
            get { return _value; }
            set { _value = value; OnPropertyChanged("Value"); }
        } 
        #endregion

        #region Tag
        private string _tag;
        public string Tag
        {
            get { return _tag; }
            set { _tag = value; OnPropertyChanged("Tag"); }
        } 
        #endregion

        #region Number
        private string _number;
        public string Number
        {
            get { return _number; }
            set { _number = value; OnPropertyChanged("Number"); }
        } 
        #endregion

        #region Cache
        private MdcCache _cache;
        public MdcCache Cache
        {
            get { return _cache; }
            set { _cache = value; OnPropertyChanged("Cache"); }
        }
        #endregion

        #region Region
        private CacheRegion _region;
        public CacheRegion Region
        {
            get { return _region; }
            set { _region = value; OnPropertyChanged("Region"); }
        } 
        #endregion

        public event EventHandler CacheItemAdded;

        public void AddCacheItem()
        {
            try
            {
                var client = CacheClientFactory.GetCache(Cache);
                if (client == null) return;

                if (!string.IsNullOrEmpty(Key) &&
                     !string.IsNullOrEmpty(Value))
                {
                    if (Region != null)
                    {
                        client.Add(Key, Value, Region.Name);
                    }
                    else
                    {
                        client.Add(Key, Value);
                    }
                }
                else
                {
                    var num = Convert.ToInt32(Number);

                    for (int i = 0; i < num; i++)
                    {
                        client.Add(Guid.NewGuid().ToString(), i);
                    }
                }
                if (CacheItemAdded != null)
                    CacheItemAdded(this, EventArgs.Empty);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
